package com.XinYun.Library.modules.person;

import com.XinYun.Library.modules.commons.BasicServlet;
import com.XinYun.Library.modules.person.Service.SqlServiceImpl;
import com.XinYun.Library.utils.HTTPRequestUtils.HTTPRequestUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "pictureServlet", value = "/pictureServlet")
public class pictureServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        设置编码方式
        String json = HTTPRequestUtil.getBody(request);
        //将JSON字符转化为JAVA对象
        System.out.println(request.getContextPath());
        JSONObject jsonObject = JSONObject.parseObject(json);
        String value = jsonObject.getString("uid");
//        设置输出
        PrintWriter outprint = response.getWriter();
//        设置文件目录
        String webroot = this.getServletContext().getRealPath("/");
        File temppath = new File(webroot + "fileuploadtemp");
        String dir = webroot+ File.separator + "upload";
        File path = new File(webroot+ File.separator + "upload");
        if (!temppath.exists()) {
            temppath.mkdirs();
        }
        if (!path.exists()) {
            path.mkdirs();
        }
//      设置文件类型(后期可新增文件类型)
        String[] type= new String[]{".jpg",".png",".jpeg",".gif"};
//      创建文件项工厂
        DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 1024,
                temppath);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(1024 * 1024 * 10);
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            Iterator<FileItem> it = fileItems.iterator();
//            遍历request file
            while (it.hasNext()) {
                FileItem fi = it.next();
//                判断该表单为普通表单类型
                if (fi.isFormField()) {
                    System.out.println("字段名：" + fi.getFieldName());
                    System.out.println("字段值：" + fi.getString());
                } else {
                    InputStream in = fi.getInputStream();
                    String name = fi.getName();//获得文件原名
//                    得到文件后缀名
                    int index = name.lastIndexOf(".");
                    String endWith = name.substring(index);
//                    判断是否符合类型
                    boolean TypeExists = Arrays.asList(type).contains(endWith);
                    if (!TypeExists) {
                        outprint.print("<script>\n" +
                                "alert(\"文件类型错误，只允许jpg,png,jpeg,gif\");location=\"fileup.html\";\n" +
                                "</script>");
                        return;
                    }

                    String newFimeName = System.currentTimeMillis() + endWith;//新文件名
//                    创建上传文件
                    FileOutputStream out = new FileOutputStream(new File(
                            dir + "/" + newFimeName));

                    byte buffer[] = new byte[1024];
                    int len = 0;
                    while ((len = in.read(buffer)) > 0) {
                        out.write(buffer, 0, len);//写入大小
                    }
                    String s = JSON.toJSONString(buffer);
                    try {
                        SqlServiceImpl.picture(s,value);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    PrintWriter writer = response.getWriter();
                    writer.println(s);
                    in.close();
                    out.close();
                    fi.delete();
                    outprint.print("上传成功");
                    jsonObject.put("code",1);
                    jsonObject.put("message","上传头像成功");
                }
            }
        } catch(FileUploadException e){
            response.getWriter().write(e.toString());
            jsonObject.put("code",-1);
            jsonObject.put("message","上传头像失败");
        }
        response.getWriter().write(jsonObject.toString());
    }
}

